package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.EvaluationLogFormalDao;
import cn.topevery.garden.dal.dao.EvaluationStatisticsDao;
import cn.topevery.garden.entity.po.EvaluationStatistics;
import cn.topevery.garden.entity.query.EvaluationStatisticsQuery;
import cn.topevery.garden.entity.vo.EvaluationStatisticsVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.basis.runtime.UserRuntime;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检查考评统计报表
 *
 * @author code-generate
 * @date 2022-01-05
 */
@Repository
public class EvaluationStatisticsDaoImpl extends BaseDaoImpl<EvaluationStatistics> implements EvaluationStatisticsDao {
    @Autowired
    private EvaluationLogFormalDao evaluationLogFormalDao;
    @Override
    public <P extends DbCsp> List<EvaluationStatistics> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<EvaluationStatisticsVO> listPage(EvaluationStatisticsQuery query) {
        String table = "t_evaluation_statistics tes";

        String columns = "tes.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tes.c_id desc");
        sqlQueryMate
                .where(true, "tes.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, EvaluationStatisticsVO.class);
    }

    @Override
    public void statisticsReportAdd(EvaluationStatistics statistics) {
        insert(UserRuntime.getSysUser().userId, statistics);
        Map<String, Object> map = new HashMap<>();
        map.put("tableId", statistics.getId());
        map.put("beginDate", statistics.getBeginDate());
        map.put("endDate", statistics.getEndDate());
        evaluationLogFormalDao.statisticsLogAdd(map);
    }

    @Override
    public DataPager<EvaluationStatistics> getPage(EvaluationStatisticsQuery query) {
        String table = "t_evaluation_statistics tes";

        String columns = "tes.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tes.c_id desc");
        sqlQueryMate
                .where(true, "tes.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getBeginDate()), "tes.c_begin_date", ScriptConditionEnum.GreaterThanEq, query.getBeginDate())
                .where(StringUtils.isNotBlank(query.getEndDate()), "tes.c_end_date", ScriptConditionEnum.LessThanEq, query.getEndDate())
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, EvaluationStatistics.class);
    }


    @Override
    public EvaluationStatistics getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

}
